package com.moxiao.yfjh.module.activity.controller.admin.packageType;

import com.moxiao.yfjh.module.activity.common.GenActiveNoCommonUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;

import com.moxiao.yfjh.module.activity.controller.admin.packageType.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageType.PackageTypeDO;
import com.moxiao.yfjh.module.activity.convert.packageType.PackageTypeConvert;
import com.moxiao.yfjh.module.activity.service.packageType.PackageTypeService;

@Tag(name = "管理后台 - 相亲套餐类型")
@RestController
@RequestMapping("/activity/package-type")
@Validated
public class PackageTypeController {

    @Resource
    private PackageTypeService packageTypeService;
    @Resource
    private GenActiveNoCommonUtil genActiveNoCommonUtil;

    @PostMapping("/create")
    @Operation(summary = "创建相亲套餐类型")
    @PreAuthorize("@ss.hasPermission('activity:package-type:create')")
    public CommonResult<Long> createPackageType(@Valid @RequestBody PackageTypeCreateReqVO createReqVO) {
        genActiveNoCommonUtil.addPackageTypeNo(createReqVO);
        return success(packageTypeService.createPackageType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲套餐类型")
    @PreAuthorize("@ss.hasPermission('activity:package-type:update')")
    public CommonResult<Boolean> updatePackageType(@Valid @RequestBody PackageTypeUpdateReqVO updateReqVO) {
        packageTypeService.updatePackageType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲套餐类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:package-type:delete')")
    public CommonResult<Boolean> deletePackageType(@RequestParam("id") Long id) {
        packageTypeService.deletePackageType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲套餐类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:package-type:query')")
    public CommonResult<PackageTypeRespVO> getPackageType(@RequestParam("id") Long id) {
        PackageTypeDO packageType = packageTypeService.getPackageType(id);
        return success(PackageTypeConvert.INSTANCE.convert(packageType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲套餐类型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:package-type:query')")
    public CommonResult<List<PackageTypeRespVO>> getPackageTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<PackageTypeDO> list = packageTypeService.getPackageTypeList(ids);
        return success(PackageTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲套餐类型分页")
    @PreAuthorize("@ss.hasPermission('activity:package-type:query')")
    public CommonResult<PageResult<PackageTypeRespVO>> getPackageTypePage(@Valid PackageTypePageReqVO pageVO) {
        PageResult<PackageTypeDO> pageResult = packageTypeService.getPackageTypePage(pageVO);
        return success(PackageTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲套餐类型 Excel")
    @PreAuthorize("@ss.hasPermission('activity:package-type:export')")
    @OperateLog(type = EXPORT)
    public void exportPackageTypeExcel(@Valid PackageTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PackageTypeDO> list = packageTypeService.getPackageTypeList(exportReqVO);
        // 导出 Excel
        List<PackageTypeExcelVO> datas = PackageTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲套餐类型.xls", "数据", PackageTypeExcelVO.class, datas);
    }

}
